
/*
===============================================================================
                    EntitySpaces Studio by EntitySpaces, LLC
             Persistence Layer and Business Objects for Microsoft .NET
             EntitySpaces(TM) is a legal trademark of EntitySpaces, LLC
                          http://www.entityspaces.net
===============================================================================
EntitySpaces Version : 2012.1.0930.0
EntitySpaces Driver  : SQL
Date Generated       : 3/29/2013 2:25:55 PM
===============================================================================
*/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Linq;
using System.Data;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Runtime.Serialization;

using EntitySpaces.Core;
using EntitySpaces.Interfaces;
using EntitySpaces.DynamicQuery;



namespace DNNspot.LiveChat.DataModel
{
	/// <summary>
	/// Encapsulates the 'DNNspot_LiveChat_Sessions' table
	/// </summary>

    [DebuggerDisplay("Data = {Debug}")]
	[Serializable]
	[DataContract]
	[KnownType(typeof(Session))]	
	[XmlType("Session")]
	public partial class Session : esSession
	{	
		[DebuggerBrowsable(DebuggerBrowsableState.RootHidden | DebuggerBrowsableState.Never)]
		protected override esEntityDebuggerView[] Debug
		{
			get { return base.Debug; }
		}

		override public esEntity CreateInstance()
		{
			return new Session();
		}
		
		#region Static Quick Access Methods
		static public void Delete(System.Int32 sessionId)
		{
			var obj = new Session();
			obj.SessionId = sessionId;
			obj.AcceptChanges();
			obj.MarkAsDeleted();
			obj.Save();
		}

	    static public void Delete(System.Int32 sessionId, esSqlAccessType sqlAccessType)
		{
			var obj = new Session();
			obj.SessionId = sessionId;
			obj.AcceptChanges();
			obj.MarkAsDeleted();
			obj.Save(sqlAccessType);
		}
		#endregion

		
					
		
	
	}



    [DebuggerDisplay("Count = {Count}")]
	[Serializable]
	[CollectionDataContract]
	[XmlType("SessionCollection")]
	public partial class SessionCollection : esSessionCollection, IEnumerable<Session>
	{
		public Session FindByPrimaryKey(System.Int32 sessionId)
		{
			return this.SingleOrDefault(e => e.SessionId == sessionId);
		}

		
		
		#region WCF Service Class
		
		[DataContract]
		[KnownType(typeof(Session))]
		public class SessionCollectionWCFPacket : esCollectionWCFPacket<SessionCollection>
		{
			public static implicit operator SessionCollection(SessionCollectionWCFPacket packet)
			{
				return packet.Collection;
			}

			public static implicit operator SessionCollectionWCFPacket(SessionCollection collection)
			{
				return new SessionCollectionWCFPacket() { Collection = collection };
			}
		}
		
		#endregion
		
				
	}



    [DebuggerDisplay("Query = {Parse()}")]
	[Serializable]	
	public partial class SessionQuery : esSessionQuery
	{
		public SessionQuery(string joinAlias)
		{
			this.es.JoinAlias = joinAlias;
		}	

		override protected string GetQueryName()
		{
			return "SessionQuery";
		}
		
					
	
		#region Explicit Casts
		
		public static explicit operator string(SessionQuery query)
		{
			return SessionQuery.SerializeHelper.ToXml(query);
		}

		public static explicit operator SessionQuery(string query)
		{
			return (SessionQuery)SessionQuery.SerializeHelper.FromXml(query, typeof(SessionQuery));
		}
		
		#endregion		
	}

	[DataContract]
	[Serializable]
	abstract public partial class esSession : esEntity
	{
		public esSession()
		{

		}
		
		#region LoadByPrimaryKey
		public virtual bool LoadByPrimaryKey(System.Int32 sessionId)
		{
			if(this.es.Connection.SqlAccessType == esSqlAccessType.DynamicSQL)
				return LoadByPrimaryKeyDynamic(sessionId);
			else
				return LoadByPrimaryKeyStoredProcedure(sessionId);
		}

		public virtual bool LoadByPrimaryKey(esSqlAccessType sqlAccessType, System.Int32 sessionId)
		{
			if (sqlAccessType == esSqlAccessType.DynamicSQL)
				return LoadByPrimaryKeyDynamic(sessionId);
			else
				return LoadByPrimaryKeyStoredProcedure(sessionId);
		}

		private bool LoadByPrimaryKeyDynamic(System.Int32 sessionId)
		{
			SessionQuery query = new SessionQuery();
			query.Where(query.SessionId == sessionId);
			return this.Load(query);
		}

		private bool LoadByPrimaryKeyStoredProcedure(System.Int32 sessionId)
		{
			esParameters parms = new esParameters();
			parms.Add("SessionId", sessionId);
			return this.Load(esQueryType.StoredProcedure, this.es.spLoadByPrimaryKey, parms);
		}
		#endregion
		
		#region Properties
		
		
		
		/// <summary>
		/// Maps to DNNspot_LiveChat_Sessions.PortalId
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.Int32? PortalId
		{
			get
			{
				return base.GetSystemInt32(SessionMetadata.ColumnNames.PortalId);
			}
			
			set
			{
				if(base.SetSystemInt32(SessionMetadata.ColumnNames.PortalId, value))
				{
					OnPropertyChanged(SessionMetadata.PropertyNames.PortalId);
				}
			}
		}		
		
		/// <summary>
		/// Maps to DNNspot_LiveChat_Sessions.SessionId
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.Int32? SessionId
		{
			get
			{
				return base.GetSystemInt32(SessionMetadata.ColumnNames.SessionId);
			}
			
			set
			{
				if(base.SetSystemInt32(SessionMetadata.ColumnNames.SessionId, value))
				{
					OnPropertyChanged(SessionMetadata.PropertyNames.SessionId);
				}
			}
		}		
		
		/// <summary>
		/// Maps to DNNspot_LiveChat_Sessions.CategoryId
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.Int32? CategoryId
		{
			get
			{
				return base.GetSystemInt32(SessionMetadata.ColumnNames.CategoryId);
			}
			
			set
			{
				if(base.SetSystemInt32(SessionMetadata.ColumnNames.CategoryId, value))
				{
					this._UpToCategoryByCategoryId = null;
					this.OnPropertyChanged("UpToCategoryByCategoryId");
					OnPropertyChanged(SessionMetadata.PropertyNames.CategoryId);
				}
			}
		}		
		
		/// <summary>
		/// Maps to DNNspot_LiveChat_Sessions.AgentId
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.Int32? AgentId
		{
			get
			{
				return base.GetSystemInt32(SessionMetadata.ColumnNames.AgentId);
			}
			
			set
			{
				if(base.SetSystemInt32(SessionMetadata.ColumnNames.AgentId, value))
				{
					this._UpToAgentByAgentId = null;
					this.OnPropertyChanged("UpToAgentByAgentId");
					OnPropertyChanged(SessionMetadata.PropertyNames.AgentId);
				}
			}
		}		
		
		/// <summary>
		/// Maps to DNNspot_LiveChat_Sessions.ClientId
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.Int32? ClientId
		{
			get
			{
				return base.GetSystemInt32(SessionMetadata.ColumnNames.ClientId);
			}
			
			set
			{
				if(base.SetSystemInt32(SessionMetadata.ColumnNames.ClientId, value))
				{
					this._UpToClientByClientId = null;
					this.OnPropertyChanged("UpToClientByClientId");
					OnPropertyChanged(SessionMetadata.PropertyNames.ClientId);
				}
			}
		}		
		
		/// <summary>
		/// Maps to DNNspot_LiveChat_Sessions.IsActive
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.Boolean? IsActive
		{
			get
			{
				return base.GetSystemBoolean(SessionMetadata.ColumnNames.IsActive);
			}
			
			set
			{
				if(base.SetSystemBoolean(SessionMetadata.ColumnNames.IsActive, value))
				{
					OnPropertyChanged(SessionMetadata.PropertyNames.IsActive);
				}
			}
		}		
		
		/// <summary>
		/// Maps to DNNspot_LiveChat_Sessions.UrlReferrer
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.String UrlReferrer
		{
			get
			{
				return base.GetSystemString(SessionMetadata.ColumnNames.UrlReferrer);
			}
			
			set
			{
				if(base.SetSystemString(SessionMetadata.ColumnNames.UrlReferrer, value))
				{
					OnPropertyChanged(SessionMetadata.PropertyNames.UrlReferrer);
				}
			}
		}		
		
		/// <summary>
		/// Maps to DNNspot_LiveChat_Sessions.Question
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.String Question
		{
			get
			{
				return base.GetSystemString(SessionMetadata.ColumnNames.Question);
			}
			
			set
			{
				if(base.SetSystemString(SessionMetadata.ColumnNames.Question, value))
				{
					OnPropertyChanged(SessionMetadata.PropertyNames.Question);
				}
			}
		}		
		
		/// <summary>
		/// Maps to DNNspot_LiveChat_Sessions.Notes
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.String Notes
		{
			get
			{
				return base.GetSystemString(SessionMetadata.ColumnNames.Notes);
			}
			
			set
			{
				if(base.SetSystemString(SessionMetadata.ColumnNames.Notes, value))
				{
					OnPropertyChanged(SessionMetadata.PropertyNames.Notes);
				}
			}
		}		
		
		/// <summary>
		/// Maps to DNNspot_LiveChat_Sessions.CreatedDate
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.DateTime? CreatedDate
		{
			get
			{
				return base.GetSystemDateTime(SessionMetadata.ColumnNames.CreatedDate);
			}
			
			set
			{
				if(base.SetSystemDateTime(SessionMetadata.ColumnNames.CreatedDate, value))
				{
					OnPropertyChanged(SessionMetadata.PropertyNames.CreatedDate);
				}
			}
		}		
		
		/// <summary>
		/// Maps to DNNspot_LiveChat_Sessions.ModifiedDate
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.DateTime? ModifiedDate
		{
			get
			{
				return base.GetSystemDateTime(SessionMetadata.ColumnNames.ModifiedDate);
			}
			
			set
			{
				if(base.SetSystemDateTime(SessionMetadata.ColumnNames.ModifiedDate, value))
				{
					OnPropertyChanged(SessionMetadata.PropertyNames.ModifiedDate);
				}
			}
		}		
		
		[CLSCompliant(false)]
		internal protected Agent _UpToAgentByAgentId;
		[CLSCompliant(false)]
		internal protected Category _UpToCategoryByCategoryId;
		[CLSCompliant(false)]
		internal protected Client _UpToClientByClientId;
		#endregion	

		#region .str() Properties
		
		public override void SetProperties(IDictionary values)
		{
			foreach (string propertyName in values.Keys)
			{
				this.SetProperty(propertyName, values[propertyName]);
			}
		}
		
		public override void SetProperty(string name, object value)
		{
			esColumnMetadata col = this.Meta.Columns.FindByPropertyName(name);
			if (col != null)
			{
				if(value == null || value is System.String)
				{				
					// Use the strongly typed property
					switch (name)
					{							
						case "PortalId": this.str().PortalId = (string)value; break;							
						case "SessionId": this.str().SessionId = (string)value; break;							
						case "CategoryId": this.str().CategoryId = (string)value; break;							
						case "AgentId": this.str().AgentId = (string)value; break;							
						case "ClientId": this.str().ClientId = (string)value; break;							
						case "IsActive": this.str().IsActive = (string)value; break;							
						case "UrlReferrer": this.str().UrlReferrer = (string)value; break;							
						case "Question": this.str().Question = (string)value; break;							
						case "Notes": this.str().Notes = (string)value; break;							
						case "CreatedDate": this.str().CreatedDate = (string)value; break;							
						case "ModifiedDate": this.str().ModifiedDate = (string)value; break;
					}
				}
				else
				{
					switch (name)
					{	
						case "PortalId":
						
							if (value == null || value is System.Int32)
								this.PortalId = (System.Int32?)value;
								OnPropertyChanged(SessionMetadata.PropertyNames.PortalId);
							break;
						
						case "SessionId":
						
							if (value == null || value is System.Int32)
								this.SessionId = (System.Int32?)value;
								OnPropertyChanged(SessionMetadata.PropertyNames.SessionId);
							break;
						
						case "CategoryId":
						
							if (value == null || value is System.Int32)
								this.CategoryId = (System.Int32?)value;
								OnPropertyChanged(SessionMetadata.PropertyNames.CategoryId);
							break;
						
						case "AgentId":
						
							if (value == null || value is System.Int32)
								this.AgentId = (System.Int32?)value;
								OnPropertyChanged(SessionMetadata.PropertyNames.AgentId);
							break;
						
						case "ClientId":
						
							if (value == null || value is System.Int32)
								this.ClientId = (System.Int32?)value;
								OnPropertyChanged(SessionMetadata.PropertyNames.ClientId);
							break;
						
						case "IsActive":
						
							if (value == null || value is System.Boolean)
								this.IsActive = (System.Boolean?)value;
								OnPropertyChanged(SessionMetadata.PropertyNames.IsActive);
							break;
						
						case "CreatedDate":
						
							if (value == null || value is System.DateTime)
								this.CreatedDate = (System.DateTime?)value;
								OnPropertyChanged(SessionMetadata.PropertyNames.CreatedDate);
							break;
						
						case "ModifiedDate":
						
							if (value == null || value is System.DateTime)
								this.ModifiedDate = (System.DateTime?)value;
								OnPropertyChanged(SessionMetadata.PropertyNames.ModifiedDate);
							break;
					

						default:
							break;
					}
				}
			}
            else if (this.ContainsColumn(name))
            {
                this.SetColumn(name, value);
            }
			else
			{
				throw new Exception("SetProperty Error: '" + name + "' not found");
			}
		}		

		public esStrings str()
		{
			if (esstrings == null)
			{
				esstrings = new esStrings(this);
			}
			return esstrings;
		}

		sealed public class esStrings
		{
			public esStrings(esSession entity)
			{
				this.entity = entity;
			}
			
	
			public System.String PortalId
			{
				get
				{
					System.Int32? data = entity.PortalId;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.PortalId = null;
					else entity.PortalId = Convert.ToInt32(value);
				}
			}
				
			public System.String SessionId
			{
				get
				{
					System.Int32? data = entity.SessionId;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.SessionId = null;
					else entity.SessionId = Convert.ToInt32(value);
				}
			}
				
			public System.String CategoryId
			{
				get
				{
					System.Int32? data = entity.CategoryId;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.CategoryId = null;
					else entity.CategoryId = Convert.ToInt32(value);
				}
			}
				
			public System.String AgentId
			{
				get
				{
					System.Int32? data = entity.AgentId;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.AgentId = null;
					else entity.AgentId = Convert.ToInt32(value);
				}
			}
				
			public System.String ClientId
			{
				get
				{
					System.Int32? data = entity.ClientId;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.ClientId = null;
					else entity.ClientId = Convert.ToInt32(value);
				}
			}
				
			public System.String IsActive
			{
				get
				{
					System.Boolean? data = entity.IsActive;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.IsActive = null;
					else entity.IsActive = Convert.ToBoolean(value);
				}
			}
				
			public System.String UrlReferrer
			{
				get
				{
					System.String data = entity.UrlReferrer;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.UrlReferrer = null;
					else entity.UrlReferrer = Convert.ToString(value);
				}
			}
				
			public System.String Question
			{
				get
				{
					System.String data = entity.Question;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.Question = null;
					else entity.Question = Convert.ToString(value);
				}
			}
				
			public System.String Notes
			{
				get
				{
					System.String data = entity.Notes;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.Notes = null;
					else entity.Notes = Convert.ToString(value);
				}
			}
				
			public System.String CreatedDate
			{
				get
				{
					System.DateTime? data = entity.CreatedDate;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.CreatedDate = null;
					else entity.CreatedDate = Convert.ToDateTime(value);
				}
			}
				
			public System.String ModifiedDate
			{
				get
				{
					System.DateTime? data = entity.ModifiedDate;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.ModifiedDate = null;
					else entity.ModifiedDate = Convert.ToDateTime(value);
				}
			}
			

			private esSession entity;
		}
		
		[NonSerialized]
		private esStrings esstrings;		
		
		#endregion
		
		#region Housekeeping methods

		override protected IMetadata Meta
		{
			get
			{
				return SessionMetadata.Meta();
			}
		}

		#endregion		
		
		#region Query Logic

		public SessionQuery Query
		{
			get
			{
				if (this.query == null)
				{
					this.query = new SessionQuery();
					InitQuery(this.query);
				}

				return this.query;
			}
		}

		public bool Load(SessionQuery query)
		{
			this.query = query;
			InitQuery(this.query);
			return this.Query.Load();
		}
		
		protected void InitQuery(SessionQuery query)
		{
			query.OnLoadDelegate = this.OnQueryLoaded;
			
			if (!query.es2.HasConnection)
			{
				query.es2.Connection = ((IEntity)this).Connection;
			}			
		}

		#endregion
		
        [IgnoreDataMember]
		private SessionQuery query;		
	}



	[Serializable]
	abstract public partial class esSessionCollection : esEntityCollection<Session>
	{
		#region Housekeeping methods
		override protected IMetadata Meta
		{
			get
			{
				return SessionMetadata.Meta();
			}
		}

		protected override string GetCollectionName()
		{
			return "SessionCollection";
		}

		#endregion		
		
		#region Query Logic

	#if (!WindowsCE)
		[BrowsableAttribute(false)]
	#endif
		public SessionQuery Query
		{
			get
			{
				if (this.query == null)
				{
					this.query = new SessionQuery();
					InitQuery(this.query);
				}

				return this.query;
			}
		}

		public bool Load(SessionQuery query)
		{
			this.query = query;
			InitQuery(this.query);
			return Query.Load();
		}

		override protected esDynamicQuery GetDynamicQuery()
		{
			if (this.query == null)
			{
				this.query = new SessionQuery();
				this.InitQuery(query);
			}
			return this.query;
		}

		protected void InitQuery(SessionQuery query)
		{
			query.OnLoadDelegate = this.OnQueryLoaded;
			
			if (!query.es2.HasConnection)
			{
				query.es2.Connection = ((IEntityCollection)this).Connection;
			}			
		}

		protected override void HookupQuery(esDynamicQuery query)
		{
			this.InitQuery((SessionQuery)query);
		}

		#endregion
		
		private SessionQuery query;
	}



	[Serializable]
	abstract public partial class esSessionQuery : esDynamicQuery
	{
		override protected IMetadata Meta
		{
			get
			{
				return SessionMetadata.Meta();
			}
		}	
		
		#region QueryItemFromName
		
        protected override esQueryItem QueryItemFromName(string name)
        {
            switch (name)
            {
				case "PortalId": return this.PortalId;
				case "SessionId": return this.SessionId;
				case "CategoryId": return this.CategoryId;
				case "AgentId": return this.AgentId;
				case "ClientId": return this.ClientId;
				case "IsActive": return this.IsActive;
				case "UrlReferrer": return this.UrlReferrer;
				case "Question": return this.Question;
				case "Notes": return this.Notes;
				case "CreatedDate": return this.CreatedDate;
				case "ModifiedDate": return this.ModifiedDate;

                default: return null;
            }
        }		
		
		#endregion
		
		#region esQueryItems

		public esQueryItem PortalId
		{
			get { return new esQueryItem(this, SessionMetadata.ColumnNames.PortalId, esSystemType.Int32); }
		} 
		
		public esQueryItem SessionId
		{
			get { return new esQueryItem(this, SessionMetadata.ColumnNames.SessionId, esSystemType.Int32); }
		} 
		
		public esQueryItem CategoryId
		{
			get { return new esQueryItem(this, SessionMetadata.ColumnNames.CategoryId, esSystemType.Int32); }
		} 
		
		public esQueryItem AgentId
		{
			get { return new esQueryItem(this, SessionMetadata.ColumnNames.AgentId, esSystemType.Int32); }
		} 
		
		public esQueryItem ClientId
		{
			get { return new esQueryItem(this, SessionMetadata.ColumnNames.ClientId, esSystemType.Int32); }
		} 
		
		public esQueryItem IsActive
		{
			get { return new esQueryItem(this, SessionMetadata.ColumnNames.IsActive, esSystemType.Boolean); }
		} 
		
		public esQueryItem UrlReferrer
		{
			get { return new esQueryItem(this, SessionMetadata.ColumnNames.UrlReferrer, esSystemType.String); }
		} 
		
		public esQueryItem Question
		{
			get { return new esQueryItem(this, SessionMetadata.ColumnNames.Question, esSystemType.String); }
		} 
		
		public esQueryItem Notes
		{
			get { return new esQueryItem(this, SessionMetadata.ColumnNames.Notes, esSystemType.String); }
		} 
		
		public esQueryItem CreatedDate
		{
			get { return new esQueryItem(this, SessionMetadata.ColumnNames.CreatedDate, esSystemType.DateTime); }
		} 
		
		public esQueryItem ModifiedDate
		{
			get { return new esQueryItem(this, SessionMetadata.ColumnNames.ModifiedDate, esSystemType.DateTime); }
		} 
		
		#endregion
		
	}


	
	public partial class Session : esSession
	{

		#region MessageCollectionBySessionId - Zero To Many
		
		static public esPrefetchMap Prefetch_MessageCollectionBySessionId
		{
			get
			{
				esPrefetchMap map = new esPrefetchMap();
				map.PrefetchDelegate = DNNspot.LiveChat.DataModel.Session.MessageCollectionBySessionId_Delegate;
				map.PropertyName = "MessageCollectionBySessionId";
				map.MyColumnName = "SessionId";
				map.ParentColumnName = "SessionId";
				map.IsMultiPartKey = false;
				return map;
			}
		}		
		
		static private void MessageCollectionBySessionId_Delegate(esPrefetchParameters data)
		{
			SessionQuery parent = new SessionQuery(data.NextAlias());

			MessageQuery me = data.You != null ? data.You as MessageQuery : new MessageQuery(data.NextAlias());

			if (data.Root == null)
			{
				data.Root = me;
			}
			
			data.Root.InnerJoin(parent).On(parent.SessionId == me.SessionId);

			data.You = parent;
		}			
		
		/// <summary>
		/// Zero to Many
		/// Foreign Key Name - FK_DNNspot_LiveChat_Messages_DNNspot_LiveChat_Sessions
		/// </summary>

		[XmlIgnore]
		public MessageCollection MessageCollectionBySessionId
		{
			get
			{
				if(this._MessageCollectionBySessionId == null)
				{
					this._MessageCollectionBySessionId = new MessageCollection();
					this._MessageCollectionBySessionId.es.Connection.Name = this.es.Connection.Name;
					this.SetPostSave("MessageCollectionBySessionId", this._MessageCollectionBySessionId);
				
					if (this.SessionId != null)
					{
						if (!this.es.IsLazyLoadDisabled)
						{
							this._MessageCollectionBySessionId.Query.Where(this._MessageCollectionBySessionId.Query.SessionId == this.SessionId);
							this._MessageCollectionBySessionId.Query.Load();
						}

						// Auto-hookup Foreign Keys
						this._MessageCollectionBySessionId.fks.Add(MessageMetadata.ColumnNames.SessionId, this.SessionId);
					}
				}

				return this._MessageCollectionBySessionId;
			}
			
			set 
			{ 
				if (value != null) throw new Exception("'value' Must be null"); 
			 
				if (this._MessageCollectionBySessionId != null) 
				{ 
					this.RemovePostSave("MessageCollectionBySessionId"); 
					this._MessageCollectionBySessionId = null;
					
				} 
			} 			
		}
			
		
		private MessageCollection _MessageCollectionBySessionId;
		#endregion

				
		#region UpToAgentByAgentId - Many To One
		/// <summary>
		/// Many to One
		/// Foreign Key Name - FK_DNNspot_LiveChat_Sessions_DNNspot_LiveChat_Agents
		/// </summary>

		[XmlIgnore]
					
		public Agent UpToAgentByAgentId
		{
			get
			{
				if (this.es.IsLazyLoadDisabled) return null;
				
				if(this._UpToAgentByAgentId == null && AgentId != null)
				{
					this._UpToAgentByAgentId = new Agent();
					this._UpToAgentByAgentId.es.Connection.Name = this.es.Connection.Name;
					this.SetPreSave("UpToAgentByAgentId", this._UpToAgentByAgentId);
					this._UpToAgentByAgentId.Query.Where(this._UpToAgentByAgentId.Query.AgentId == this.AgentId);
					this._UpToAgentByAgentId.Query.Load();
				}	
				return this._UpToAgentByAgentId;
			}
			
			set
			{
				this.RemovePreSave("UpToAgentByAgentId");
				

				if(value == null)
				{
					this.AgentId = null;
					this._UpToAgentByAgentId = null;
				}
				else
				{
					this.AgentId = value.AgentId;
					this._UpToAgentByAgentId = value;
					this.SetPreSave("UpToAgentByAgentId", this._UpToAgentByAgentId);
				}
				
			}
		}
		#endregion
		

				
		#region UpToCategoryByCategoryId - Many To One
		/// <summary>
		/// Many to One
		/// Foreign Key Name - FK_DNNspot_LiveChat_Sessions_DNNspot_LiveChat_Categories
		/// </summary>

		[XmlIgnore]
					
		public Category UpToCategoryByCategoryId
		{
			get
			{
				if (this.es.IsLazyLoadDisabled) return null;
				
				if(this._UpToCategoryByCategoryId == null && CategoryId != null)
				{
					this._UpToCategoryByCategoryId = new Category();
					this._UpToCategoryByCategoryId.es.Connection.Name = this.es.Connection.Name;
					this.SetPreSave("UpToCategoryByCategoryId", this._UpToCategoryByCategoryId);
					this._UpToCategoryByCategoryId.Query.Where(this._UpToCategoryByCategoryId.Query.CategoryId == this.CategoryId);
					this._UpToCategoryByCategoryId.Query.Load();
				}	
				return this._UpToCategoryByCategoryId;
			}
			
			set
			{
				this.RemovePreSave("UpToCategoryByCategoryId");
				

				if(value == null)
				{
					this.CategoryId = null;
					this._UpToCategoryByCategoryId = null;
				}
				else
				{
					this.CategoryId = value.CategoryId;
					this._UpToCategoryByCategoryId = value;
					this.SetPreSave("UpToCategoryByCategoryId", this._UpToCategoryByCategoryId);
				}
				
			}
		}
		#endregion
		

				
		#region UpToClientByClientId - Many To One
		/// <summary>
		/// Many to One
		/// Foreign Key Name - FK_DNNspot_LiveChat_Sessions_DNNspot_LiveChat_Clients
		/// </summary>

		[XmlIgnore]
					
		public Client UpToClientByClientId
		{
			get
			{
				if (this.es.IsLazyLoadDisabled) return null;
				
				if(this._UpToClientByClientId == null && ClientId != null)
				{
					this._UpToClientByClientId = new Client();
					this._UpToClientByClientId.es.Connection.Name = this.es.Connection.Name;
					this.SetPreSave("UpToClientByClientId", this._UpToClientByClientId);
					this._UpToClientByClientId.Query.Where(this._UpToClientByClientId.Query.ClientId == this.ClientId);
					this._UpToClientByClientId.Query.Load();
				}	
				return this._UpToClientByClientId;
			}
			
			set
			{
				this.RemovePreSave("UpToClientByClientId");
				

				if(value == null)
				{
					this.ClientId = null;
					this._UpToClientByClientId = null;
				}
				else
				{
					this.ClientId = value.ClientId;
					this._UpToClientByClientId = value;
					this.SetPreSave("UpToClientByClientId", this._UpToClientByClientId);
				}
				
			}
		}
		#endregion
		

		
		protected override esEntityCollectionBase CreateCollectionForPrefetch(string name)
		{
			esEntityCollectionBase coll = null;

			switch (name)
			{
				case "MessageCollectionBySessionId":
					coll = this.MessageCollectionBySessionId;
					break;	
			}

			return coll;
		}		
		/// <summary>
		/// Used internally by the entity's hierarchical properties.
		/// </summary>
		protected override List<esPropertyDescriptor> GetHierarchicalProperties()
		{
			List<esPropertyDescriptor> props = new List<esPropertyDescriptor>();
			
			props.Add(new esPropertyDescriptor(this, "MessageCollectionBySessionId", typeof(MessageCollection), new Message()));
		
			return props;
		}
		/// <summary>
		/// Used internally for retrieving AutoIncrementing keys
		/// during hierarchical PreSave.
		/// </summary>
		protected override void ApplyPreSaveKeys()
		{
			if(!this.es.IsDeleted && this._UpToAgentByAgentId != null)
			{
				this.AgentId = this._UpToAgentByAgentId.AgentId;
			}
			if(!this.es.IsDeleted && this._UpToCategoryByCategoryId != null)
			{
				this.CategoryId = this._UpToCategoryByCategoryId.CategoryId;
			}
			if(!this.es.IsDeleted && this._UpToClientByClientId != null)
			{
				this.ClientId = this._UpToClientByClientId.ClientId;
			}
		}
		
		/// <summary>
		/// Called by ApplyPostSaveKeys 
		/// </summary>
		/// <param name="coll">The collection to enumerate over</param>
		/// <param name="key">"The column name</param>
		/// <param name="value">The column value</param>
		private void Apply(esEntityCollectionBase coll, string key, object value)
		{
			foreach (esEntity obj in coll)
			{
				if (obj.es.IsAdded)
				{
					obj.SetProperty(key, value);
				}
			}
		}
		
		/// <summary>
		/// Used internally for retrieving AutoIncrementing keys
		/// during hierarchical PostSave.
		/// </summary>
		protected override void ApplyPostSaveKeys()
		{
			if(this._MessageCollectionBySessionId != null)
			{
				Apply(this._MessageCollectionBySessionId, "SessionId", this.SessionId);
			}
		}
		
	}
	



	[Serializable]
	public partial class SessionMetadata : esMetadata, IMetadata
	{
		#region Protected Constructor
		protected SessionMetadata()
		{
			m_columns = new esColumnMetadataCollection();
			esColumnMetadata c;

			c = new esColumnMetadata(SessionMetadata.ColumnNames.PortalId, 0, typeof(System.Int32), esSystemType.Int32);
			c.PropertyName = SessionMetadata.PropertyNames.PortalId;
			c.NumericPrecision = 10;
			c.IsNullable = true;
			m_columns.Add(c);
				
			c = new esColumnMetadata(SessionMetadata.ColumnNames.SessionId, 1, typeof(System.Int32), esSystemType.Int32);
			c.PropertyName = SessionMetadata.PropertyNames.SessionId;
			c.IsInPrimaryKey = true;
			c.IsAutoIncrement = true;
			c.NumericPrecision = 10;
			m_columns.Add(c);
				
			c = new esColumnMetadata(SessionMetadata.ColumnNames.CategoryId, 2, typeof(System.Int32), esSystemType.Int32);
			c.PropertyName = SessionMetadata.PropertyNames.CategoryId;
			c.NumericPrecision = 10;
			c.IsNullable = true;
			m_columns.Add(c);
				
			c = new esColumnMetadata(SessionMetadata.ColumnNames.AgentId, 3, typeof(System.Int32), esSystemType.Int32);
			c.PropertyName = SessionMetadata.PropertyNames.AgentId;
			c.NumericPrecision = 10;
			c.IsNullable = true;
			m_columns.Add(c);
				
			c = new esColumnMetadata(SessionMetadata.ColumnNames.ClientId, 4, typeof(System.Int32), esSystemType.Int32);
			c.PropertyName = SessionMetadata.PropertyNames.ClientId;
			c.NumericPrecision = 10;
			c.IsNullable = true;
			m_columns.Add(c);
				
			c = new esColumnMetadata(SessionMetadata.ColumnNames.IsActive, 5, typeof(System.Boolean), esSystemType.Boolean);
			c.PropertyName = SessionMetadata.PropertyNames.IsActive;
			c.HasDefault = true;
			c.Default = @"((1))";
			m_columns.Add(c);
				
			c = new esColumnMetadata(SessionMetadata.ColumnNames.UrlReferrer, 6, typeof(System.String), esSystemType.String);
			c.PropertyName = SessionMetadata.PropertyNames.UrlReferrer;
			c.CharacterMaxLength = 512;
			c.IsNullable = true;
			m_columns.Add(c);
				
			c = new esColumnMetadata(SessionMetadata.ColumnNames.Question, 7, typeof(System.String), esSystemType.String);
			c.PropertyName = SessionMetadata.PropertyNames.Question;
			c.CharacterMaxLength = 1073741823;
			c.IsNullable = true;
			m_columns.Add(c);
				
			c = new esColumnMetadata(SessionMetadata.ColumnNames.Notes, 8, typeof(System.String), esSystemType.String);
			c.PropertyName = SessionMetadata.PropertyNames.Notes;
			c.CharacterMaxLength = 1073741823;
			c.IsNullable = true;
			m_columns.Add(c);
				
			c = new esColumnMetadata(SessionMetadata.ColumnNames.CreatedDate, 9, typeof(System.DateTime), esSystemType.DateTime);
			c.PropertyName = SessionMetadata.PropertyNames.CreatedDate;
			c.HasDefault = true;
			c.Default = @"(getdate())";
			m_columns.Add(c);
				
			c = new esColumnMetadata(SessionMetadata.ColumnNames.ModifiedDate, 10, typeof(System.DateTime), esSystemType.DateTime);
			c.PropertyName = SessionMetadata.PropertyNames.ModifiedDate;
			c.HasDefault = true;
			c.Default = @"(getdate())";
			m_columns.Add(c);
				
		}
		#endregion	
	
		static public SessionMetadata Meta()
		{
			return meta;
		}	
		
		public Guid DataID
		{
			get { return base.m_dataID; }
		}	
		
		public bool MultiProviderMode
		{
			get { return false; }
		}		

		public esColumnMetadataCollection Columns
		{
			get	{ return base.m_columns; }
		}
		
		#region ColumnNames
		public class ColumnNames
		{ 
			 public const string PortalId = "PortalId";
			 public const string SessionId = "SessionId";
			 public const string CategoryId = "CategoryId";
			 public const string AgentId = "AgentId";
			 public const string ClientId = "ClientId";
			 public const string IsActive = "IsActive";
			 public const string UrlReferrer = "UrlReferrer";
			 public const string Question = "Question";
			 public const string Notes = "Notes";
			 public const string CreatedDate = "CreatedDate";
			 public const string ModifiedDate = "ModifiedDate";
		}
		#endregion	
		
		#region PropertyNames
		public class PropertyNames
		{ 
			 public const string PortalId = "PortalId";
			 public const string SessionId = "SessionId";
			 public const string CategoryId = "CategoryId";
			 public const string AgentId = "AgentId";
			 public const string ClientId = "ClientId";
			 public const string IsActive = "IsActive";
			 public const string UrlReferrer = "UrlReferrer";
			 public const string Question = "Question";
			 public const string Notes = "Notes";
			 public const string CreatedDate = "CreatedDate";
			 public const string ModifiedDate = "ModifiedDate";
		}
		#endregion	

		public esProviderSpecificMetadata GetProviderMetadata(string mapName)
		{
			MapToMeta mapMethod = mapDelegates[mapName];

			if (mapMethod != null)
				return mapMethod(mapName);
			else
				return null;
		}
		
		#region MAP esDefault
		
		static private int RegisterDelegateesDefault()
		{
			// This is only executed once per the life of the application
			lock (typeof(SessionMetadata))
			{
				if(SessionMetadata.mapDelegates == null)
				{
					SessionMetadata.mapDelegates = new Dictionary<string,MapToMeta>();
				}
				
				if (SessionMetadata.meta == null)
				{
					SessionMetadata.meta = new SessionMetadata();
				}
				
				MapToMeta mapMethod = new MapToMeta(meta.esDefault);
				mapDelegates.Add("esDefault", mapMethod);
				mapMethod("esDefault");
			}
			return 0;
		}			

		private esProviderSpecificMetadata esDefault(string mapName)
		{
			if(!m_providerMetadataMaps.ContainsKey(mapName))
			{
				esProviderSpecificMetadata meta = new esProviderSpecificMetadata();			


				meta.AddTypeMap("PortalId", new esTypeMap("int", "System.Int32"));
				meta.AddTypeMap("SessionId", new esTypeMap("int", "System.Int32"));
				meta.AddTypeMap("CategoryId", new esTypeMap("int", "System.Int32"));
				meta.AddTypeMap("AgentId", new esTypeMap("int", "System.Int32"));
				meta.AddTypeMap("ClientId", new esTypeMap("int", "System.Int32"));
				meta.AddTypeMap("IsActive", new esTypeMap("bit", "System.Boolean"));
				meta.AddTypeMap("UrlReferrer", new esTypeMap("nvarchar", "System.String"));
				meta.AddTypeMap("Question", new esTypeMap("nvarchar", "System.String"));
				meta.AddTypeMap("Notes", new esTypeMap("nvarchar", "System.String"));
				meta.AddTypeMap("CreatedDate", new esTypeMap("datetime", "System.DateTime"));
				meta.AddTypeMap("ModifiedDate", new esTypeMap("datetime", "System.DateTime"));			
				
				
				
				meta.Source = "DNNspot_LiveChat_Sessions";
				meta.Destination = "DNNspot_LiveChat_Sessions";
				
				meta.spInsert = "proc_DNNspot_LiveChat_SessionsInsert";				
				meta.spUpdate = "proc_DNNspot_LiveChat_SessionsUpdate";		
				meta.spDelete = "proc_DNNspot_LiveChat_SessionsDelete";
				meta.spLoadAll = "proc_DNNspot_LiveChat_SessionsLoadAll";
				meta.spLoadByPrimaryKey = "proc_DNNspot_LiveChat_SessionsLoadByPrimaryKey";
				
				this.m_providerMetadataMaps["esDefault"] = meta;
			}
			
			return this.m_providerMetadataMaps["esDefault"];
		}

		#endregion

		static private SessionMetadata meta;
		static protected Dictionary<string, MapToMeta> mapDelegates;
		static private int _esDefault = RegisterDelegateesDefault();
	}
}
